package com.yaoli.ajob;

/**
 * Created by will on 2017/7/8.
 */
public class _606_ConstructStringfromBinaryTree {
    public String tree2str(TreeNode t) {
        return calc(t);
    }

    public String calc(TreeNode node){
        if(node != null){
            int val = node.val;
            String leftStr = "";
            String rightStr ="";

            if(node.left != null || node.right != null){
                if(node.left != null && node.right != null){
                    leftStr = "("+calc(node.left)+")";
                    rightStr = "("+calc(node.right)+")";
                }
                if(node.left != null && node.right == null){
                    leftStr = "("+calc(node.left)+")";
                    rightStr = "";
                }
                if(node.left == null && node.right != null){
                    leftStr = "()";
                    rightStr = "("+calc(node.right)+")";
                }
                return val + leftStr + rightStr;
            }else{
                return ""+val;
            }

        }else{
            return "";
        }
    }
}
